package cn.ht.jywrwater.module.ht.controller.app.gtdata;

import cn.ht.jywrwater.framework.common.exception.ServiceException;
import cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.meterdata.vo.MeterDataCreateReqVO;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualitydata.vo.*;
import cn.ht.jywrwater.module.ht.convert.waterqualitydata.WaterQualityDataConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.meterdata.MeterDataDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydata.WaterQualityDataDO;
import cn.ht.jywrwater.module.ht.dal.mysql.meterdata.MeterDataMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualitydata.WaterQualityDataMapper;
import cn.ht.jywrwater.module.ht.service.waterqualitydata.WaterQualityDataService;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "馆陶数据 - 数据对接")
@RestController
@RequestMapping("/ht/gtdata")
@Validated
public class GtDataController {

    @Resource
    private WaterQualityDataService waterQualityDataService;

    @Resource
    private WaterQualityDataMapper waterQualityDataMapper;

    @Resource
    private MeterDataMapper meterDataMapper;

    private static byte[] KEY = {0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8};
    private static AES DATA_AES = new AES(KEY);


    @GetMapping("/water-quality-from-gt")
    @Operation(summary = "gt水质数据")
    public CommonResult<Boolean> createWaterQualityDataGt(@RequestParam("waterQualityData") String waterQualityData) {
        try {
            String oriData = DATA_AES.decryptStr(waterQualityData);

            WaterQualityDataDO bean = JSONUtil.toBean(oriData, WaterQualityDataDO.class);
            waterQualityDataMapper.insert(bean);
        } catch (Exception e) {
            throw new ServiceException(1000, e.getMessage());
        }

        return success(true);
    }

    @GetMapping("/meter-data-from-gt")
    @Operation(summary = "gt水表数据")
    public CommonResult<Boolean> createMeterData(@RequestParam("meterData") String meterData) {
        String oriData = DATA_AES.decryptStr(meterData);
        try {
            MeterDataDO bean = JSONUtil.toBean(oriData, MeterDataDO.class);
            meterDataMapper.insert(bean);
        } catch (Exception e) {
            throw ServiceExceptionUtil.exception(1000, e.getMessage());
        }

        return success(true);
    }


}
